/*
 *@(#)HomeImpl.java 1.0 Feb 1, 2008
 *Copyright Fution Bai
 *
 */
package codebush.service.impl;

import java.io.File;
import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.transaction.annotation.Transactional;

import codebush.domain.Users;
import codebush.service.Home;
import codebush.service.support.CodebushUtilSupport;
import codebush.util.HttpUtil;

/**
 * 
 * @author Fution Bai
 * @since 1.0
 */
public class HomeImpl extends SimpleJdbcDaoSupport implements Home {

	private LobHandler lobHandler;
	private CodebushUtilSupport codebushUtilSupport;

	public void setLobHandler(LobHandler lobHandler) {
		this.lobHandler = lobHandler;
	}

	public void setCodebushUtilSupport(CodebushUtilSupport codebushUtilSupport) {
		this.codebushUtilSupport = codebushUtilSupport;
	}

	/**
	 * post an item
	 */
	@Transactional
	public void postArticle(String title, String content, String tag, int type, int userId) {

		String sql = "call sp_post_article(?,?,?,?,?)";
		CallableStatement statement = HttpUtil.getCallableStatement(getDataSource(), sql);
		try {
			statement.setString(1, title);
			statement.setString(2, content);
			statement.setString(3, tag);
			statement.setInt(4, type);
			statement.setInt(5, userId);
			statement.execute("set names utf8");
			statement.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void postArticle(String title, String content, String tag, int userId) {
		postArticle(title, content, tag, 0, userId);
	}

	public void showProfile(int userId) {
	}

	/**
	 * store the original picture in table image contentLength is in bytes
	 */
	@Transactional
	public void storeMyPicture(final int userId, final InputStream contentStream, final int contentLength) {

		getJdbcTemplate().execute("INSERT INTO image (user_id, image, size) VALUES (?, ?, ?)", new AbstractLobCreatingPreparedStatementCallback(this.lobHandler) {
			protected void setValues(PreparedStatement ps, LobCreator lobCreator) {
				try {
					ps.setInt(1, userId);
					lobCreator.setBlobAsBinaryStream(ps, 2, contentStream, contentLength);
					ps.setInt(3, contentLength);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * store the compressed picture in database column users.picture
	 * contentLength is in bytes
	 */
	@Transactional
	public void storeCompressedMyPicture(final int userId, final InputStream contentStream, final int contentLength) {

		getJdbcTemplate().execute("update users set picture=? where id=?", new AbstractLobCreatingPreparedStatementCallback(this.lobHandler) {
			protected void setValues(PreparedStatement ps, LobCreator lobCreator) {
				try {
					lobCreator.setBlobAsBinaryStream(ps, 1, contentStream, contentLength);
					ps.setInt(2, userId);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});

	}

	/**
	 * store the file in the local file system.
	 * @param file
	 * @param userId
	 */
	public void storeInFileSystem(File fromFile, int userId) {
		this.codebushUtilSupport.storeCompressedMyPictureInFileSystem(fromFile, userId);
	}

	/**
	 * modify an article
	 */
	@Transactional
	public void modifyArticle(long articleId, String title, String content, String tag, int type) {
		if (tag != null && !tag.equals("")) {
			int tag_id = getJdbcTemplate().queryForInt("select id from tag where name='" + tag + "'");
			getJdbcTemplate().execute("update article_tag set tag_id='" + tag_id + "' where article_id='" + articleId + "'");
		}
		String sql = "update article set title=?, content=?, type=? where id=?";
		Object[] args = { title, content, type, articleId };
		getJdbcTemplate().update(sql, args);
	}

	/**
	 * delete an article
	 */
	@Transactional
	public void deleteArticle(final long articleId) {
		String sql = "call sp_delete_article(?)";
		getJdbcTemplate().execute(sql, new PreparedStatementCallback() {
			public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
				ps.setLong(1, articleId);
				ps.execute();
				return null;
			}
		});
	}

	/**
	 * delete an account
	 */
	@Transactional
	public void deleteAccount(final int userId) {
		String sql = "call sp_delete_account(?)";
		getJdbcTemplate().execute(sql, new PreparedStatementCallback() {
			public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
				ps.setInt(1, userId);
				ps.execute();
				return null;
			}
		});
	}

	/**
	 * 完善用户的个人信息
	 */
	public void profileFill(Users user) {
		String sql = "update users set sex=?, current_place=? where id=?";
		Object[] args = { user.isSex(), user.getCurrent_place(), user.getId() };
		getJdbcTemplate().update(sql, args);
	}
}
